﻿<!DOCTYPE html>
<html>
  <head>
    <!-- meta name="viewport" content="width=device-width, height=device-height, user-scalable=yes, initial-scale=2.0, maximum-scale=4.0, minimum-scale=1.0" / -->
    <meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no;" />
    <meta http-equiv="Content-type" content="text/html; charset=utf-8"> <!-- ISO-8859-1 -->
    <title>Cordova</title>
    <link rel="stylesheet" href="master.css" type="text/css" media="screen">

    <script type="text/javascript" charset="utf-8" src="cordova-1.7.0.js"></script>


<script type="text/javascript" charset="utf-8">


    //-------------------------------------------------------------------------
    // Audio player
    //-------------------------------------------------------------------------
    var media1 = null;
    var media1Timer = null;
    var audioSrc = null;
    var recordSrc = "/folder/1.wav";

    /**
     * Play audio
     */
    function playAudio(path) {
        console.log("playAudio()");
        console.log(" -- media="+media1);
        var src = path;

        // Stop playing if src is different from currently playing source
        if (src != audioSrc) {
            if (media1 != null) {
                stopAudio();
                media1 = null;
            }
        }

        if (media1 == null) {
            console.log("Start creating media...");
            media1 = new Media(src,
                function() {
                    console.log("playAudio():Audio Success");
                },
                function(err) {
                    console.log("playAudio():Audio Error: "+err);
                    setAudioStatus("Error: " + err);
                },
                function(status) {
                    console.log("playAudio():Audio Status: "+status);
                    setAudioStatus(Media.MEDIA_MSG[status]);

                    // If stopped, then stop getting current position
                    if (Media.MEDIA_STOPPED == status) {
                        clearInterval(media1Timer);
                        media1Timer = null;
                    }
                });
        }
        console.log("Start play audio...");
        audioSrc = src;
        document.getElementById('audio_duration').innerHTML = "";
        // Play audio
        media1.play();

        document.getElementById('play_caption').innerHTML = "Now playing: ";
        document.getElementById('file_name').innerHTML = path;

        if (media1Timer == null && media1.getCurrentPosition) {
            media1Timer = setInterval(
                function() {
                    media1.getCurrentPosition(
                        function(position) {
                            console.log("Pos="+position);
                            if (position > -1) {
                                setAudioPosition(position+" sec");
                            }
                        },
                        function(e) {
                            console.log("Error getting pos="+e);
                            setAudioPosition("Error: "+e);
                        }
                    );
                },
                1000
            );
        }

        // Get duration
        var counter = 0;
        var timerDur = setInterval(
            function() {
                counter = counter + 100;
                if (counter > 2000) {
                    clearInterval(timerDur);
                }
                var dur = media1.getDuration();
                if (dur > 0) {
                    clearInterval(timerDur);
                    document.getElementById('audio_duration').innerHTML = dur + " sec";
                }
            }, 100);

    }

    /**
     * Pause audio playback
     */
    function pauseAudio() {
        console.log("pauseAudio()");
        if (media1) {
            media1.pause();
        }
    }

    /**
     * Stop audio
     */
    function stopAudio() {
        console.log("stopAudio()");
        if (media1) {
            media1.stop();
        }
        clearInterval(media1Timer);
        media1Timer = null;
    }

    /**
     * Set audio status
     */
    var setAudioStatus = function(status) {
        document.getElementById('audio_status').innerHTML = status;
    };

    /**
     * Set audio position
     */
    var setAudioPosition = function(position) {
        document.getElementById('audio_position').innerHTML = position;
    };

    //-------------------------------------------------------------------------
    // Audio recorder
    //-------------------------------------------------------------------------
    var mediaRec = null;
    var recTime = 0;

    /**
     * Record audio
     */
    function recordAudio() {
        console.log("recordAudio()");
        console.log(" -- media="+mediaRec);
        if (mediaRec == null) {

            var src = recordSrc;
            mediaRec = new Media(src,
                    function() {
                        console.log("recordAudio():Audio Success");
                    },
                    function(err) {
                        console.log("recordAudio():Audio Error: "+err);
                        setAudioStatus("Error: " + err);
                    },
                    function(status) {
                        console.log("recordAudio():Audio Status: "+status);
                        setAudioStatus(Media.MEDIA_MSG[status]);
                    }
                );
        }

        navigator.notification.beep(1);

        console.log("Start recording...");
        mediaRec.startRecord();

        document.getElementById('play_caption').innerHTML = "Recording in:";
        document.getElementById('file_name').innerHTML = recordSrc;

        // Stop recording after 10 sec
        recTime = 0;
        var recInterval = setInterval(function() {
            recTime = recTime + 1;
            setAudioPosition(recTime+" sec");
            if (recTime >= 10) {
                clearInterval(recInterval);
                if (mediaRec.stopAudioRecord){
                    console.log("stopAudioRecord");
                    mediaRec.stopAudioRecord();
                } else {
                    console.log("stopRecord");
                    mediaRec.stopRecord();
                }
                console.log("recordAudio(): stop");
                navigator.notification.beep(1);
            }
        }, 1000);
    }

    /**
     * Play back recorded audio
     */
    function playRecording() {
        playAudio(recordSrc);
    }

    function seekTo() {
        console.log("seekTo()");
        if (media1) {
            media1.seekTo(15000);
        }
    }

    /**
     * Function called when page has finished loading.
     */
    function init() {
        document.addEventListener("deviceready", function() {
                deviceReady = true;
                console.log("Device="+device.platform+" "+device.version);
            }, false);
    }

</script>

  </head>
  <body onLoad="init();" id="stage" class="theme">

    <h1>Audio</h1>
    <div id="info">
        <table width="100%">
        <tr><td id="play_caption">Now playing:</td><td id="file_name"> </td></tr>
        <tr><td>Status:</td><td id="audio_status"> </td></tr>
        <tr><td>Duration:</td><td id="audio_duration"></td></tr>
        <tr><td>Position:</td><td id="audio_position"></td></tr>
        </table>
    </div>
    <h2>Action</h2>
    <a href="#" class="btn large" onClick="playAudio('http://audio.ibeat.org/content/p1rj1s/p1rj1s_-_rockGuitar.mp3');">Play/Unpause Audio</a>
    <a href="#" class="btn large" onClick="pauseAudio();">Pause Playing Audio</a>
    <a href="#" class="btn large" onClick="stopAudio();">Stop Playing Audio</a>
    <a href="#" class="btn large" onClick="recordAudio();">Record Audio for 10 sec</a>
    <a href="#" class="btn large" onClick="playRecording();">Play/Unpause Recording</a>
    <a href="#" class="btn large" onClick="seekTo();">Seek To 15 sec</a>
      <h2> </h2><a href="index.html" class="backBtn">Back</a>

  </body>
</html>